package com.admin.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.admin.bean.BillingBean;
import com.admin.bean.BillingSpBean;
import com.admin.bean.BillingSpVo;
import com.admin.bean.SpBean;
import com.admin.service.BillingService;
import com.admin.service.SpService;
import com.admin.util.BaseUtil;
import com.admin.util.PageBean;

@Controller
@RequestMapping("/billingSp")
public class BillingSpAction {

	@Autowired private SpService spService;
	@Autowired private BillingService billingService;
	
	@RequestMapping("/{billingId}")
	public String list(@PathVariable int billingId,HttpServletRequest request){
		BillingBean billing = billingService.findBillingById(billingId);
		StringBuffer url = new StringBuffer(billingId+"?1=1");
		int provinceId = BaseUtil.stringToInt(request.getParameter("provinceId"),-1);
		if(provinceId>=0){
			request.setAttribute("provinceId", provinceId);
			url.append("&provinceId="+provinceId);
		}
		int isp = BaseUtil.stringToInt(request.getParameter("isp"), -1);
		if(isp>=0){
			request.setAttribute("isp", isp);
			url.append("&isp="+isp);
		}
		int vodCustomized = BaseUtil.stringToInt(request.getParameter("vodCustomized"), -1);
		if(vodCustomized>=0){
			request.setAttribute("vodCustomized", vodCustomized);
			url.append("&vodCustomized="+vodCustomized);
		}
		int totalCount = spService.findCountByCondition(provinceId, isp, vodCustomized);
		int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
		PageBean<BillingSpVo> pageBean = new PageBean<BillingSpVo>(totalCount, currentPage,10);
		List<SpBean> spList = spService.findSpBeanByCondition(provinceId, isp, vodCustomized, pageBean.getStart(), pageBean.getPageSize());
		if(spList!=null && spList.size()>0){
			List<BillingSpVo> billingSpVoList = new ArrayList<BillingSpVo>(spList.size());
			for(SpBean sp: spList){
				BillingSpVo vo = new BillingSpVo();
				vo.setSpBean(sp);
				vo.setBillingSpBean(spService.findBillingSpBySPIdAndBillingId(sp.getSpId(), billingId));
				billingSpVoList.add(vo);
			}
			pageBean.setResult(billingSpVoList);
		}
		pageBean.setUrl(url.toString());
		request.setAttribute("pageBean", pageBean);
		request.setAttribute("billing", billing);
		request.setAttribute("billingId", billingId);
		return "billingSpAdd";
	}
	
	@RequestMapping("/hasjoin/{billingId}")
	public String showHasBd(@PathVariable int billingId,HttpServletRequest request){
		int currentPage = BaseUtil.stringToInt(request.getParameter("currentPage"),1);
		int totalCount = spService.findCountByBillingId(billingId);
		PageBean<BillingSpVo> pageBean = new PageBean<BillingSpVo>(totalCount, currentPage,15);
		List<BillingSpVo> spVoList = spService.findSpByBillingId(billingId,pageBean.getStart(),pageBean.getPageSize());
		pageBean.setResult(spVoList);
		request.setAttribute("pageBean", pageBean);
		request.setAttribute("billingId", billingId);
		return "billingSpList";
	}
	
	@RequestMapping("/join/{billingId}")
	public ModelAndView join(@PathVariable int billingId,HttpServletRequest request,HttpServletResponse response) throws IOException{
		int spId = BaseUtil.stringToInt(request.getParameter("spId"));
		int count = BaseUtil.stringToInt(request.getParameter("count"), 1);
		BillingSpBean billingSp = new BillingSpBean();
		billingSp.setBillingId(billingId);
		billingSp.setSpId(spId);
		billingSp.setProvinceId(spService.findSpById(spId).getProvinceId());
		billingSp.setCount(count);
		int result = spService.insertBillingSp(billingSp);
		response.setContentType("text/html");
		response.getWriter().print(result);
		return null;
	}
	
	@RequestMapping("/del/{id}")
	public ModelAndView del(@PathVariable int id,HttpServletRequest request,HttpServletResponse response) throws IOException{
		int result = spService.delBillingSp(id);
		response.setContentType("text/html");
		response.getWriter().print(result);
		return null;
	}
}
